﻿using System;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Data.OleDb;

namespace 自消费
{
    [Serializable]
    public class CardTaskData
    {

        public const string CARD_IDField = "@card_id";

        public const string COMMPORTField = "@commport";

        public const string ENDTIMEField = "@endtime";

        public const string EXEC_RESULTField = "@exec_result";

        public const string FAIL_REASONField = "@fail_reason";

        public const string IDField = "@ID";

        public const string PRIMARYKEY_Field = @"ID";

        public const string IMEIField = "@imei";

        public const string STARTTIMEField = "@starttime";

        public const string SUCCEEDField = "@succeed";

        public const string TASK_CONTENTField = "@task_content";

        public const string TASK_NAMEField = "@task_name";

        public const string TASK_TYPEField = "@task_type";

        private String _card_id;

        private Int32 _commport;

        private DateTime _endtime;

        private String _exec_result;

        private String _fail_reason;

        private Int32 _ID;

        private String _imei;

        private DateTime _starttime;

        private Boolean _succeed;

        private String _task_content;

        private String _task_name;

        private Int32 _task_type;

        public String card_id
        {
            get { return _card_id; }
            set { _card_id = value; }
        }
        public Int32 commport
        {
            get { return _commport; }
            set { _commport = value; }
        }
        public DateTime endtime
        {
            get { return _endtime; }
            set { _endtime = value; }
        }
        public String exec_result
        {
            get { return _exec_result; }
            set { _exec_result = value; }
        }
        public String fail_reason
        {
            get { return _fail_reason; }
            set { _fail_reason = value; }
        }
        public Int32 ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public String imei
        {
            get { return _imei; }
            set { _imei = value; }
        }
        public DateTime starttime
        {
            get { return _starttime; }
            set { _starttime = value; }
        }
        public Boolean succeed
        {
            get { return _succeed; }
            set { _succeed = value; }
        }
        public String task_content
        {
            get { return _task_content; }
            set { _task_content = value; }
        }
        public String task_name
        {
            get { return _task_name; }
            set { _task_name = value; }
        }
        public Int32 task_type
        {
            get { return _task_type; }
            set { _task_type = value; }
        }
        public CardTaskData()
        {
            _card_id = "";
            _commport = 0;
            _endtime = DateTime.Now;
            _exec_result = "";
            _fail_reason = "";
            _ID = 0;
            _imei = "";
            _starttime = DateTime.Now;
            _succeed = false;
            _task_content = "";
            _task_name = "";
            _task_type = 0;
        }
    }

    public class CardTaskDataFactory
    {
        public static CardTaskData Construct(IDataReader reader)
        {
            CardTaskData data = new CardTaskData();

            try { data.card_id = Convert.ToString(reader["card_id"]); }
            catch { }

            try { data.commport = Convert.ToInt32(reader["commport"]); }
            catch { }

            try { data.endtime = Convert.ToDateTime(reader["endtime"]); }
            catch { }

            try { data.exec_result = Convert.ToString(reader["exec_result"]); }
            catch { }

            try { data.fail_reason = Convert.ToString(reader["fail_reason"]); }
            catch { }

            try { data.ID = Convert.ToInt32(reader["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(reader["imei"]); }
            catch { }

            try { data.starttime = Convert.ToDateTime(reader["starttime"]); }
            catch { }

            try { data.succeed = Convert.ToBoolean(reader["succeed"]); }
            catch { }

            try { data.task_content = Convert.ToString(reader["task_content"]); }
            catch { }

            try { data.task_name = Convert.ToString(reader["task_name"]); }
            catch { }

            try { data.task_type = Convert.ToInt32(reader["task_type"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static Int32 GetPrimaryKey(DataTable dt, int rowIndex)
        {
            Int32 PrimaryKey = 0;

            if (dt.Rows.Count < rowIndex + 1)

                return 0;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return 0;

            try { PrimaryKey = Convert.ToInt32(dr[CardTaskData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static CardTaskData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static CardTaskData Construct(DataTable dt, int rowIndex)
        {
            CardTaskData data = new CardTaskData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.card_id = Convert.ToString(dr["card_id"]); }
            catch { }

            try { data.commport = Convert.ToInt32(dr["commport"]); }
            catch { }

            try { data.endtime = Convert.ToDateTime(dr["endtime"]); }
            catch { }

            try { data.exec_result = Convert.ToString(dr["exec_result"]); }
            catch { }

            try { data.fail_reason = Convert.ToString(dr["fail_reason"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(dr["imei"]); }
            catch { }

            try { data.starttime = Convert.ToDateTime(dr["starttime"]); }
            catch { }

            try { data.succeed = Convert.ToBoolean(dr["succeed"]); }
            catch { }

            try { data.task_content = Convert.ToString(dr["task_content"]); }
            catch { }

            try { data.task_name = Convert.ToString(dr["task_name"]); }
            catch { }

            try { data.task_type = Convert.ToInt32(dr["task_type"]); }
            catch { }

            return data;
        }
        public static CardTaskData Construct(DataGridView dgv, int rowIndex)
        {
            CardTaskData data = new CardTaskData();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            try { data.card_id = Convert.ToString(dr["card_id"]); }
            catch { }

            try { data.commport = Convert.ToInt32(dr["commport"]); }
            catch { }

            try { data.endtime = Convert.ToDateTime(dr["endtime"]); }
            catch { }

            try { data.exec_result = Convert.ToString(dr["exec_result"]); }
            catch { }

            try { data.fail_reason = Convert.ToString(dr["fail_reason"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(dr["imei"]); }
            catch { }

            try { data.starttime = Convert.ToDateTime(dr["starttime"]); }
            catch { }

            try { data.succeed = Convert.ToBoolean(dr["succeed"]); }
            catch { }

            try { data.task_content = Convert.ToString(dr["task_content"]); }
            catch { }

            try { data.task_name = Convert.ToString(dr["task_name"]); }
            catch { }

            try { data.task_type = Convert.ToInt32(dr["task_type"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataGridView dgv, int rowIndex)
        {
            Int32 PrimaryKey = 0;

            if (dgv.Rows.Count < rowIndex + 1)

                return 0;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return 0;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                if (dgv.Columns[i].DataPropertyName == CardTaskData.PRIMARYKEY_Field)
                {
                    try { PrimaryKey = Convert.ToInt32(dr[i].Value); }
                    catch { }
                    return PrimaryKey;
                }
            }
            return PrimaryKey;
        }
        public static CardTaskData Construct(DataRow dr)
        {
            CardTaskData data = new CardTaskData();

            try { data.card_id = Convert.ToString(dr["card_id"]); }
            catch { }

            try { data.commport = Convert.ToInt32(dr["commport"]); }
            catch { }

            try { data.endtime = Convert.ToDateTime(dr["endtime"]); }
            catch { }

            try { data.exec_result = Convert.ToString(dr["exec_result"]); }
            catch { }

            try { data.fail_reason = Convert.ToString(dr["fail_reason"]); }
            catch { }

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.imei = Convert.ToString(dr["imei"]); }
            catch { }

            try { data.starttime = Convert.ToDateTime(dr["starttime"]); }
            catch { }

            try { data.succeed = Convert.ToBoolean(dr["succeed"]); }
            catch { }

            try { data.task_content = Convert.ToString(dr["task_content"]); }
            catch { }

            try { data.task_name = Convert.ToString(dr["task_name"]); }
            catch { }

            try { data.task_type = Convert.ToInt32(dr["task_type"]); }
            catch { }

            return data;
        }
        public static Int32 GetPrimaryKey(DataRow dr)
        {
            Int32 PrimaryKey = 0;

            try { PrimaryKey = Convert.ToInt32(dr[CardTaskData.PRIMARYKEY_Field]); }
            catch { }
            return PrimaryKey;
        }

    }
    public class CardTaskDataDBOption
    {
        public static int Insert(CardTaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into CardTask (");
            sql.Append("card_id, ");
            sql.Append("commport, ");
            sql.Append("endtime, ");
            sql.Append("exec_result, ");
            sql.Append("fail_reason, ");
            sql.Append("imei, ");
            sql.Append("starttime, ");
            sql.Append("succeed, ");
            sql.Append("task_content, ");
            sql.Append("task_name, ");
            sql.Append("task_type");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.card_id.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.commport.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("#");
            sql.Append(d.endtime.ToString());
            sql.Append("#");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.exec_result.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.fail_reason.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.imei.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("#");
            sql.Append(d.starttime.ToString());
            sql.Append("#");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.succeed.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.task_content.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.task_name.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.task_type.ToString());
            sql.Append("");
            sql.Append(")");
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static int Update(CardTaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update CardTask set ");
            sql.Append("card_id=");
            sql.Append("'");
            sql.Append(d.card_id.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("commport=");
            sql.Append("");
            sql.Append(d.commport.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("endtime=");
            sql.Append("#");
            sql.Append(d.endtime.ToString());
            sql.Append("#");
            sql.Append(", ");
            sql.Append("exec_result=");
            sql.Append("'");
            sql.Append(d.exec_result.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("fail_reason=");
            sql.Append("'");
            sql.Append(d.fail_reason.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("imei=");
            sql.Append("'");
            sql.Append(d.imei.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("starttime=");
            sql.Append("#");
            sql.Append(d.starttime.ToString());
            sql.Append("#");
            sql.Append(", ");
            sql.Append("succeed=");
            sql.Append("");
            sql.Append(d.succeed.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("task_content=");
            sql.Append("'");
            sql.Append(d.task_content.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("task_name=");
            sql.Append("'");
            sql.Append(d.task_name.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("task_type=");
            sql.Append("");
            sql.Append(d.task_type.ToString());
            sql.Append("");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static int Delete(CardTaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from CardTask ");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.AccessOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect(CardTaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from CardTask ");
            return WFNetLib.ADO.AccessOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect(CardTaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from CardTask ");
            return WFNetLib.ADO.AccessOP.DataSetSQL(sql.ToString());
        }
        public static OleDbDataReader ReaderSelect(CardTaskData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from CardTask ");
            return WFNetLib.ADO.AccessOP.ReaderExecuteSQL(sql.ToString());
        }
    }
}